<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Service\Base\Manage;

class NewsManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'News';
    }

    // 列表
    // 添加
    // 修改

    // 软删除
    protected function toSoft()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 推荐
    public function onTop()
    {
        $result = $this->toChange('is_top', [0, 1]);

        return $result;
    }

    // 评论列表

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [['verify', '>=', 0]];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $s_id    = $option->getData('student_id', 0, 'intval');
        $is_top  = $option->getData('is_top', null, 'intval');
        $_verify = $option->getData('verify', null, 'intval');
        $_type   = $option->getData('event_type', null, 'trim');
        $title   = $option->getData('title', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if(!is_null($is_top)){
            $bang[]  = 'is_top';
            $where[] = ['is_top', '=', $is_top];
        }
        if(!is_null($_verify)){
            $bang[]  = 'verify';
            $where[] = ['verify', '=', $_verify];
        }
        if($title){
            $bang    = false;
            $where[] = ['title', 'like', "%".$title."%"];
        }
        if($keyword){
            $bang    = false;
            $where[] = $this->orWhere('title|description|address', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'title'       => $opt->getData("title", null, "trim"),
            'uid'         => $opt->getData("uid", null, "intval"),
            'cover'       => $opt->getData("cover", null, "trim"),
            'keyword'     => $opt->getData("keyword", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'images'      => $opt->getData("images", null, "trim"),
            'verify'      => $opt->getData("verify", 1, "intval"),
            //'ip'          => $opt->getData("ip", null, "trim"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            'is_top'      => $opt->getData("is_top", null, "intval"),
            'push_time'   => $opt->getData("push_time", LARAVEL_TIME, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'title'       => $opt->getData("title", null, "trim"),
            'uid'         => $opt->getData("uid", null, "intval"),
            'cover'       => $opt->getData("cover", null, "trim"),
            'keyword'     => $opt->getData("keyword", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'images'      => $opt->getData("images", null, "trim"),
            'verify'      => $opt->getData("verify", 1, "intval"),
            //'ip'          => $opt->getData("ip", null, "trim"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            'is_top'      => $opt->getData("is_top", null, "intval"),
            'push_time'   => $opt->getData("push_time", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

}


